package template

import (
	"context"
	"encoding/json"
	"fmt"
	"github.com/hibiken/asynq"
)

const TypeServerInfo = "server:info"
const TypeOfferInfo = "offer:info"

func NewServerTask(payload map[string]interface{}) (*asynq.Task, error) {
	data, err := json.Marshal(payload)
	if err != nil {
		return nil, err
	}
	return asynq.NewTask(TypeServerInfo, data, asynq.MaxRetry(10)), nil
}

func HandleServerTask(ctx context.Context, t *asynq.Task) error {
	p := make(map[string]interface{})
	if err := json.Unmarshal(t.Payload(), &p); err != nil {
		return fmt.Errorf("json.Unmarshal failed: %v: %w", err, asynq.SkipRetry)
	}
	fmt.Printf(" [*] Exec Task: %+v\n", p)
	return nil
	//log.Printf(" [*] Attempting to Send Welcome Email to User %d...", p.UserID)
	//return fmt.Errorf("could not send email to the user") // <-- Return error
}

func NewOfferTask(payload map[string]interface{}) (*asynq.Task, error) {
	data, err := json.Marshal(payload)
	if err != nil {
		return nil, err
	}
	return asynq.NewTask(TypeOfferInfo, data, asynq.MaxRetry(10)), nil
}

func HandleOfferTask(ctx context.Context, t *asynq.Task) error {
	p := make(map[string]interface{})
	if err := json.Unmarshal(t.Payload(), &p); err != nil {
		return fmt.Errorf("json.Unmarshal failed: %v: %w", err, asynq.SkipRetry)
	}
	fmt.Printf(" [*] Exec Task: %+v\n", p)
	return nil
	//log.Printf(" [*] Attempting to Send Welcome Email to User %d...", p.UserID)
	//return fmt.Errorf("could not send email to the user") // <-- Return error
}
